﻿Imports System.Security.Cryptography
Imports System.Collections.Generic
Imports System.Data
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Xml
Imports System
Imports System.Windows.Forms
Public Class frmUpdateNet

    Public mBol As Boolean = False
    Private mDescription As String = ""
    Private mNetAdress As String = ""
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim mStr As String = "http://www.eia65.com/UserManage/AutoUpdater.xml" '版本文件

        '获得网络程序的版本号
        Dim LastVersion As String = ""

        If GetTheLastUpdateVersion(mStr, LastVersion) = False Then '"http://www.eia65.com/UserManage/NAutoUpdater.xml"
            mBol = False
        ElseIf VersionCompare(LastVersion, mDescription, mNetAdress) = False Then
            mBol = False
        Else
            mBol = True
        End If
    End Sub


    Public Sub DoCompareVersion()
        BackgroundWorker1.RunWorkerAsync()
    End Sub


    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        If mBol = True Then
            Me.Label1.Text = mDescription
            Me.Width = Me.Label1.Width + 40
            Me.ShowDialog()
        End If

    End Sub

    ''' <summary>
    ''' 版本比较，如果网络连接获得成功，返回True
    ''' </summary>
    ''' <param name="mNetVersion">网络版本号</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function VersionCompare(ByVal mNetVersion As String, ByRef mDescription As String, ByRef mNetAdress As String) As Boolean
        Dim Urlstrs As New List(Of String)
        Urlstrs.Add("http://www.eia65.com/index.php?option=com_product&lid=6&Itemid=28")
        Urlstrs.Add("http://www.era65.com/index.php?option=com_product&lid=6&Itemid=28")
        Urlstrs.Add("http://sjzwrj.vip4.eshimai.com/index.php?option=com_product&lid=6&Itemid=28")
        ''获得本机程序的版本号  
        Dim mVersion As String = My.Application.Info.Version.ToString
        If mVersion <> mNetVersion Then
            If mNetVersion.Trim = "" Then
            Else
                Dim A As Integer
                Dim B As Integer
                A = MidVersion(mVersion)
                B = MidVersion(mNetVersion)

                If B > A Then
                    Dim Req As WebRequest
                    Dim Resp As WebResponse
                    For i As Integer = 0 To Urlstrs.Count - 1
                        Dim url As New Uri(Urlstrs.Item(i)) '定义一个URI
                        Req = WebRequest.Create(url)
                        '试探N个网站的连接
                        Try
                            Resp = Req.GetResponse '获得一个响应
                            mNetAdress = Urlstrs.Item(i)
                            'System.Diagnostics.Process.Start(Urlstrs.Item(i))
                            mDescription = "本程序当前版本为:" & mVersion & vbCrLf
                            mDescription += "本程序已更新到 " & mNetVersion & " 版本,请到 http://www.eia65.com 下载新版本程序,是否进行下载更新!"
                            Return True
                            Exit For
                        Catch ex As Exception
                            mDescription = ""
                        End Try
                    Next
                End If
            End If
        End If
    End Function

    Private Function MidVersion(ByVal mVersion As String) As Integer
        Dim A As Integer = 0
        Dim B As String = ""
        B = Mid(mVersion, 1, 1) + Mid(mVersion, 3, 1) + Mid(mVersion, 5, 1)
        A = B
        Return A
    End Function
    Private Function GetTheLastUpdateVersion(ByVal Urlstrs As String, ByRef LastVersion As String) As Boolean
        Try
            Dim url As New Uri(Urlstrs) '定义一个URI
            Dim Req As WebRequest
            Req = WebRequest.Create(url)
            Dim Resp As WebResponse
            Resp = Req.GetResponse '获得一个响应

            '打开xml文件 
            Dim myReader As StreamReader = New StreamReader(Resp.GetResponseStream)
            Dim xml As XmlTextReader = New XmlTextReader(myReader)  'xml文件阅读器 
            While (xml.Read())
                If xml.Name = "Version" Then
                    '获取升级文档的最后一次更新版本
                    LastVersion = xml.GetAttribute("Num")
                    Exit While
                End If
            End While
            xml.Close()
            myReader.Close()

            Return True
        Catch ex As Exception
            LastVersion = ""
            Return False
        End Try
    End Function

    Private Sub ButY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButY.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
        System.Diagnostics.Process.Start(mNetAdress)
        Me.Close()
    End Sub

    Private Sub ButN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButN.Click
        Me.Close()
    End Sub
End Class